package com.hnichr.ump.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hnichr.ump.common.doc.entity.GroupUser;
import com.hnichr.ump.common.doc.service.GroupUserService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/doc/groupuser/v1/")
@Tag(name = "GroupUserController", description = "文印商城群组用户接口")
public class GroupUserController {
	@Autowired
	private GroupUserService groupUserService;

	/**
	 * 群组成员是否可重复
	 */
	@Value("${system.group.repeated:true}")
	private boolean repeated;

	@GetMapping("list")
	@Operation(summary = "群组成员列表", description = "获取群组成员列表（无分页）", method = "GET")
	public Result<List<GroupUser>> list(@Parameter(description = "查询参数") Criteria criteria) {
		List<GroupUser> list = groupUserService.list(GroupUser.class, criteria);
		return Result.OK(list);
	}

	@PostMapping("add")
	@Operation(summary = "新增群组成员", description = "新增群组成员", method = "POST")
	public Result<GroupUser> add(@RequestBody GroupUser groupUser) {
		if (!repeated) {
			// 先校验群组下是否有同职责用户
			Criteria criteria = Criteria.create();
			criteria.addCriterion("userId", Operator.EQ, groupUser.getUserId());
			criteria.addCriterion("groupId", Operator.EQ, groupUser.getGroupId());
			List<GroupUser> users = groupUserService.list(GroupUser.class, criteria);
			if (CollectionUtil.isNotEmpty(users)) {
				return Result.error("该用户已存在群组中");
			}
			criteria = Criteria.create();
			criteria.addCriterion("groupId", Operator.EQ, groupUser.getGroupId());
			criteria.addCriterion("userType", Operator.EQ, groupUser.getUserType());
			users = groupUserService.list(GroupUser.class, criteria);
			if (CollectionUtil.isNotEmpty(users)) {
				return Result.error("该群组已存在相同职责的用户");
			}
		}
		groupUserService.save(groupUser);
		return Result.OK(groupUser);
	}

	@PutMapping("edit")
	@Operation(summary = "编辑群组成员", description = "编辑群组成员", method = "PUT")
	public Result<GroupUser> edit(@RequestBody GroupUser groupUser) {
		// 先校验群组下是否有同职责用户
		Criteria criteria = Criteria.create();
		criteria.addCriterion("userId", Operator.EQ, groupUser.getUserId());
		criteria.addCriterion("groupId", Operator.EQ, groupUser.getGroupId());
		List<GroupUser> users = groupUserService.list(GroupUser.class, criteria);
		if (CollectionUtil.isNotEmpty(users)) {
			return Result.error("该用户已存在群组中");
		}
		criteria = Criteria.create();
		criteria.addCriterion("groupId", Operator.EQ, groupUser.getGroupId());
		criteria.addCriterion("userType", Operator.EQ, groupUser.getUserType());
		users = groupUserService.list(GroupUser.class, criteria);
		if (CollectionUtil.isNotEmpty(users)) {
			return Result.error("该群组已存在相同职责的用户");
		}
		groupUserService.update(groupUser);
		return Result.OK(groupUser);
	}

	@DeleteMapping("remove/{id}")
	@Operation(summary = "根据id删除群组成员", description = "根据id删除群组成员", method = "DELETE")
	public Result<GroupUser> remove(@PathVariable(name = "id", required = true) String id) {
		groupUserService.delete(GroupUser.class, id);
		return Result.OK();
	}
}
